package com.kerrykidz.authority.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.authority.dao.mapper.AuSecRoleEntityMapper;
import com.kerrykidz.authority.dao.model.GblSecObjEntity;
import com.kerrykidz.authority.dao.model.SecRole2objEntity;
import com.kerrykidz.authority.dao.model.SecRoleEntity;
import com.kerrykidz.authority.service.IAuRoleService;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.GenerationUUID;
import com.rdp.util.model.TreeModel;
@Service
public class AuRoleServiceImpl implements IAuRoleService{

	@Autowired
	private AuSecRoleEntityMapper roleMapper;
	
	@Override
	public void findGblSecObjByPid(String pid,List<TreeModel> resultList) throws RDPException {
		List<TreeModel> dataList = roleMapper.getGblSecObjByPid(pid);
		if(dataList != null && dataList.size() > 0){
			for(TreeModel tree : dataList){
				resultList.add(tree);
				if(tree.getNodeCount() > 0){
					//tree.setOpen(true);
					findGblSecObjByPid(tree.getId(), resultList);
				}
			}
		}
	}

	@Override
	public List<SecRoleEntity> findByRoleWhere(SecRoleEntity roleEntity)throws RDPException {
		return roleMapper.getByRoleWhere(roleEntity);
	}

	@Override
	public SecRoleEntity findByRoleId(SecRoleEntity roleEntity)throws RDPException {
		SecRoleEntity paraEntity = new SecRoleEntity();
		paraEntity.setPkRoleId(roleEntity.getPkRoleId());
		List<SecRoleEntity> resultRoleList = roleMapper.getByRoleWhere(paraEntity);
		if(resultRoleList != null && resultRoleList.size() == 1){
			return resultRoleList.get(0);
		}
		return null;
	}
	
	@Override
	public void saveRole(SecRoleEntity roleEntity,List<SecRole2objEntity> secObj) throws RDPException {
		roleEntity.setPkRoleId(GenerationUUID.getGenerationUUID());
		roleEntity.setModiTime(new Date());
		roleEntity.setDelFlag(SecRoleEntity.DEL_FLAG_FALSE);
		roleMapper.insertRole(roleEntity);
		if(secObj != null && secObj.size() > 0){
			roleMapper.insertRoleObj(secObj,roleEntity.getPkRoleId());
		}
	}


	@Override
	public void removeRoleObj(String fkRoleId) throws RDPException {
		roleMapper.deleteRoleObj(fkRoleId);
	}

	@Override
	public List<SecRole2objEntity> findByIdRoleObj(String fkRoleId)throws RDPException {
		return roleMapper.getByIdRoleObj(fkRoleId);
	}

	@Override
	public SecRoleEntity updateRole(SecRoleEntity roleEntity,List<SecRole2objEntity> secObj) throws RDPException {
		SecRoleEntity dbRole = findByRoleId(roleEntity);
		if(dbRole != null ){
			BeanUtils.copyProperties(roleEntity, dbRole);
			
			/**删除role关联对象*/
			roleMapper.deleteRoleObj(dbRole.getPkRoleId());
			
			/**新增role关联对象*/
			roleMapper.insertRoleObj(secObj,roleEntity.getPkRoleId());
			
			roleMapper.updateRole(dbRole);
			return dbRole;
		}
		return null;
	}

	@Override
	public void removeRole(SecRoleEntity roleEntity) throws RDPException {
		roleMapper.deleteRole(roleEntity);
	}

	@Override
	public SecRoleEntity findByRoleName(String roleName) throws RDPException {
		return roleMapper.getByRoleName(roleName);
	}


}
